<?php

ini_set("display_errors",0); 

print'<pre>';


if($_POST['code']) {
	$file = 'class.php.sample';
	$somecontent = print_r($_POST['code'], TRUE);
	$somecontent = json_decode($somecontent);
	$fp = fopen($file, 'w') or die('Could not open file!');
	fwrite($fp, "$somecontent") or die('Could not write to file');
	fclose($fp);
}

if($_POST['dump']) {
	copy('dump1.sample','class.php.sample');
}

$m4 = file_get_contents('config.m4.sample');
$h = file_get_contents('php_droid.h.sample');
$c = file('droid.c.sample');

require_once('classParse.php');
//$parse = new classParse('Test1.php');

$parse = new classParse('class.php.sample');
$class_name = $parse->getClassNames();
$result = $parse->getResult();

//print_r($result);

foreach($result->functions as $key=>$value) {
    
    $zval_code = null;
    $method_parse = null;
    
    //===================generate arguments=====================================
    
    if($result->arguments[$key][0] != null) {
        
        $method_parse .= 'if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "';
           if($result->arguments[$key][0] != null) $method_parse .='z';
           if($result->arguments[$key][1] != null) $method_parse .='z';

$method_parse .= '", ';   
        
        foreach($result->arguments[$key] as $k2=>$v2) {
    
            $zval_code .= 'zval* '.$result->arguments[$key][$k2].";\n    ";
            
            if($k2<count($result->arguments[$key])-1) {
                $method_parse .= '&'.$result->arguments[$key][$k2].', '; 
            } else {
                $method_parse .= '&'.$result->arguments[$key][$k2]; 
            }
        }
        
        $method_parse .=') == FAILURE) {
        RETURN_NULL();
    }';
        
        
    } else {
        $method_code = '';
    }
    
    //===================generate getters=======================================

    if($result->code[$key][0] != null) {
     
        $method_return = null;    
        //$zval_code = null;    
        
        foreach($result->code[$key] as $k2=>$v2) {
            if(preg_match('/return[\$]this/', $result->code[$key][$k2])) {
                    $v = preg_replace ("/return[\$]this->/", '', $result->code[$key][$k2]);
                    $v = preg_replace ("/;/", '', $v);
                    
                    $zval_code .= 'zval* '.$v.";\n    ";
                    
                    $method_return .= $v.' = zend_read_property(Z_OBJCE_P(This),This,"'.$v.'",strlen("'.$v.'"),1 TSRMLS_CC); 
    
    RETURN_ZVAL('.$v.',NULL,NULL);';
                    
                    
                    ;
            }
        
        }
        
        $zval_code .= 'zval* This = getThis()'.";\n    ";

    }
    
    //===================generate setters=======================================
    $method_setter = null;
    $any_setter = null;
    if($result->code[$key][0] != null) {
        foreach($result->code[$key] as $k2=>$v2) {

            if(preg_match('/^[\$]this->/', $result->code[$key][$k2])) {
                
             //           print_r($result->code[$key]);
                // $this->variable
                $v = preg_replace ("/[\$]this->/", '', $result->code[$key][$k2]);
                $v = preg_replace ("/=(.*)/", '', $v);
                
                // = $variable
                $v2 = preg_replace ("/(.*)=[\$]/", '', $result->code[$key][$k2]);
                $v2 = preg_replace ("/[\;]/", '', $v2);
                
                $method_setter .= 
                    'zend_update_property(Z_OBJCE_P(This),This,"'.$v.'",strlen("'.$v.'"),'.$v2.' TSRMLS_CC);'."\n    ";
            }
        
                // $variable = 
            if(preg_match('/[\$](.*)=/', $result->code[$key][$k2])) {
                if(!preg_match('/[\$]this->/', $result->code[$key][$k2])) {
                    $v3 = preg_replace ("/[\$]/", '', $result->code[$key][$k2]);                         
                    $v3 = preg_replace ("/=(.*)/", '', $v3);

                    $v4 = preg_replace ("/(.*)=/", '', $result->code[$key][$k2]);  
                    $v4 = preg_replace ("/[\;]/", '', $v4);  
                    $v4 = preg_replace ("/[\';]/", '', $v4);  

                    if($v3 != null) $any_setter .= 'zval* '.$v3.';'."\n".'    '.$v3.' = "'.$v4.'";'."\n".'    ';
                }
            }
            
            if(preg_match('/print/', $result->code[$key][$k2])) {
                    $v3 = preg_replace ("/print/", '', $result->code[$key][$k2]);                         
                    $v3 = preg_replace ("/[\']/", '', $v3);
                    $v3 = preg_replace ("/[;']/", '', $v3);
                    
                    if($v3 != null) $any_setter .= 'php_printf("%s", "'.$v3.'");    ';
                
            }
        }
    }
     
    
    
    //================================parse code================================
    
    $method_setter = null;
    if($result->code[$key][0] != null) {
        foreach($result->code[$key] as $k2=>$v2) {
            if(preg_match('/^[\$]this->/', $result->code[$key][$k2])) {
                
             //           print_r($result->code[$key]);
                
                $v = preg_replace ("/[\$]this->/", '', $result->code[$key][$k2]);
                $v = preg_replace ("/=(.*)/", '', $v);
                $v2 = preg_replace ("/(.*)=[\$]/", '', $result->code[$key][$k2]);
                $v2 = preg_replace ("/[\;]/", '', $v2);
                $method_setter .= 
                    'zend_update_property(Z_OBJCE_P(This),This,"'.$v.'",strlen("'.$v.'"),'.$v2.' TSRMLS_CC);'."\n    ";
            }
        }
    }
    //==========================================================================
    
    if($result->functions[$key]->type == 'public') $result->functions[$key]->type = 'ZEND_ACC_PUBLIC';
    if($result->functions[$key]->type == 'private') $result->functions[$key]->type = 'ZEND_ACC_PRIVATE';
    if($result->functions[$key]->type == 'protected') $result->functions[$key]->type = 'ZEND_ACC_PROTECTED';

    if($result->functions[$key]->name != '__construct') $any_setter = null;
    
    $php_method .= 'PHP_METHOD('.$class_name[0].', '.$result->functions[$key]->name.')
{
    '.$zval_code.'
    '.$method_parse.'
    '.$method_setter.'
    '.$any_setter.'
    '.$method_return.'
}

';
    $php_me .= 'PHP_ME('.$class_name[0].','.$result->functions[$key]->name.',NULL, '.$result->functions[$key]->type.')
';
    
}

$init_class_entry = 'INIT_CLASS_ENTRY(stubClassEntry,"'.$class_name[0].'",droid_functions);
g_ClassEntry = zend_register_internal_class(&stubClassEntry TSRMLS_CC);';

foreach($c as $key=>$value) {
    $c[$key] = preg_replace('/\@php_method/', $php_method, $c[$key]);
    $c[$key] = preg_replace('/\@php_me/', $php_me, $c[$key]);
    $c[$key] = preg_replace('/\@init_class_entry/', $init_class_entry, $c[$key]);
}

file_put_contents('./c/config.m4', $m4);
file_put_contents('./c/php_droid.h', $h);
file_put_contents('./c/droid.c', $c);

chmod('./c/config.m4', 0777);
chmod('./c/php_droid.h', 0777);
chmod('./c/droid.c', 0777);

print file_get_contents('./c/droid.c');
